package ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import shape.Shape;
import shape.ShapeColourDecorator;
import storage.CareTaker;
import surface.CanvasImpl;
import surface.CanvasObserver;
import brush.BrushFactory;

import com.google.inject.Inject;

@SuppressWarnings("serial")
public class Gui extends JFrame implements CanvasObserver {

	private final CanvasImpl canvas;
	private final transient CareTaker careTaker;
	private Color colour;
	private String shape;

	@Inject
	public Gui(final CareTaker careTaker, final CanvasImpl canvas) {
		this.canvas = canvas;
		this.careTaker = careTaker;
		canvas.attach(this);

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}

	private void createAndShowGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addComponents();
		setSize(500, 500);
		setVisible(true);
	}

	private void addComponents() {
		JPanel toolPanel = new JPanel(new FlowLayout());
		toolPanel.add(circleButton());
		toolPanel.add(fillButton());
		toolPanel.add(squareButton());

		JPanel editPanel = new JPanel(new FlowLayout());
		editPanel.add(undoButton());
		
		JPanel colourPanel = new JPanel(new FlowLayout());
		colourPanel.add(redButton());
		colourPanel.add(greenButton());
		colourPanel.add(blueButton());

		setLayout(new BorderLayout());
		add(toolPanel, BorderLayout.NORTH);
		add(canvas, BorderLayout.CENTER);
		add(editPanel, BorderLayout.EAST);
		add(colourPanel, BorderLayout.SOUTH);
	}

	private JButton undoButton() {
		JButton button = new JButton("undo");
		button.addActionListener(e -> {
			canvas.restoreMemento(careTaker.remove());
		});
		return button;
	}

	private JButton circleButton() {
		JButton button = new JButton("circle");
		button.addActionListener(e -> {
			shape = "circle";
		});
		return button;
	}
	
	private JButton squareButton() {
		JButton button = new JButton("square");
		button.addActionListener(e -> {
			shape = "square";
		});
		return button;
	}
	
	private JButton redButton() {
		JButton button = new JButton("red");
		button.addActionListener(e -> {
			colour = Color.red;
		});
		return button;
	}
	
	private JButton greenButton() {
		JButton button = new JButton("green");
		button.addActionListener(e -> {
			colour = Color.green;
		});
		return button;
	}

	private JButton blueButton() {
		JButton button = new JButton("blue");
		button.addActionListener(e -> {
			colour = Color.blue;
		});
		return button;
	}
	
	private JButton fillButton() {
		JButton button = new JButton("fill");
		button.addActionListener(e -> {
			int index = (int) (Math.random()*canvas.getShapes().size());
			Shape shape = canvas.getShape(index);
			shape = ShapeColourDecorator.decorate(shape, colour);
			canvas.updateShape(index, shape);
		});
		return button;
	}

	@Override
	public void update(boolean mouseReleased) {
		if (mouseReleased) {
			BrushFactory.getBrush(shape, canvas, canvas.getStartX(),
					canvas.getStartY(), canvas.getEndX()-canvas.getStartX()).draw();;
		}
	}
}
